package threadcommunication;

/**
 * 当线程呈wait()状态时，调用线程对象的interrupt()方法会出现InterruptedException异常
 */
public class Demo_03 {
    public static void main(String[] args) {
        try {
            Object lock = new Object();
            Thread_03 t = new Thread_03(lock);
            t.start();
            Thread.sleep(5000);
            t.interrupt();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Service {
    public void testMethod(Object lock) {
        try {
            synchronized (lock) {
                System.out.println("begin wait");
                lock.wait();
                System.out.println(" end wait");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread_03 extends Thread {
    private Object lock;

    public Thread_03(Object lock) {
        this.lock = lock;
    }

    @Override
    public void run() {
        Service service = new Service();
        service.testMethod(lock);
    }
}


